import Vue from 'vue'
import Vuex from 'vuex'
// 使用插件 (use)
Vue.use(Vuex)
const moduleA = {
  state: {
    bobby: 'bootball'
  }
}
// 创建一个变量存储创建的store实例
const store = new Vuex.Store({
  // Vuex中几个基本属性下面会详细讲解
  state: {
    // 存放的键值对就是将来要进行公共管理的状态
    name: 'helloVuex',
    counter: 10,
    info: {
      id: 15,
      name: 'tianchao',
      age: '30'
    }
  },
  actions: {
    aUpdateInfo (context, address) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          context.commit('updateInfo', address)
          resolve('success')
        }, 1000)
      })
    },
    updateName (context, name) {
      context.commit('updateName', name)
    }
  },
  mutations: {
    updateName (state, name) {
      state.name = name
    },
    // 方法
    increment (state) {
      state.counter++
    },
    decrement (state) {
      state.counter--
    },
    updateInfo (state, address) {
      // console.log(state, age , age2)
      Vue.set(state.info, 'address', address)
      // state.info['address'] = address
    }
  },
  getters: {
    powerCounter (state) {
      return state.counter * state.counter
    },
    name (state) {
      return state.name + ' getters'
    }
  },
  modules: {
    a: moduleA
  }
})
export default store
